package day24;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * PictureUploadServer.java
 * @author wangbiao<br/>
 *         email:785497344@qq.com
 * @description 图片上传的服务端
 * @since 1.0.0
 * @date 2013-7-12下午11:50:58
 *
 */
public class PictureUploadServer {

	public static void main(String[] args) {
		ServerSocket ss= null;
		FileOutputStream fos = null;
		Socket s = null;
		try {
			ss = new ServerSocket(10000);
			s = ss.accept();
			String ip = s.getInetAddress().getHostAddress();
			System.out.println(ip+"connect!");
			
			InputStream in = s.getInputStream();
			fos = new FileOutputStream("d:\\500.jpg");
			
			int len = 0;
			byte[] buf = new byte[1024];
			while ((len=in.read(buf)) != -1) {
				fos.write(buf, 0, len);
			}
			
			OutputStream out = s.getOutputStream();
			out.write("图片上传成功！".getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != fos) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != s) {
				try {
					s.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != ss) {
				try {
					ss.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
